#include "xgame_data.h"
#include <xlog.h>
#include <xbit.h>
#include "support\user_default\CCUserDefault.h"
#include <xbase64.h>
namespace zdh
{
	USING_NS_CC;
	//--------------------------------------------------------------------------------------
	XDataBase::XDataBase() :m_Change(false)
	{

	}
	//--------------------------------------------------------------------------------------
	XDataBase::XDataBase(const XDataBase & paramData) : m_Change(paramData.m_Change)
	{

	}
	//--------------------------------------------------------------------------------------
	bool XDataBase::isChanged() const
	{
		return m_Change;
	}
	//--------------------------------------------------------------------------------------
	void XDataBase::CleanChange()
	{
		m_Change = false;
	}
	//--------------------------------------------------------------------------------------
	void XDataBase::setChanged(bool paramFlag)
	{
		m_Change = paramFlag;
	}

	//--------------------------------------------------------------------------------------
	XOptionData::XOptionData()
	{

	}
	//--------------------------------------------------------------------------------------
	XOptionData::XOptionData(const XUInt32 & paramData) :XData<XUInt32>(paramData)
	{

	}
	//--------------------------------------------------------------------------------------
	bool XOptionData::getOptionFlag(int paramFlag) const
	{
		return bit::GetBitFlag32(get(), paramFlag);
	}
	//--------------------------------------------------------------------------------------
	void XOptionData::setOptionFlag(int paramFlag, bool paramFlagValue)
	{
		if (getOptionFlag(paramFlag) != paramFlagValue)
		{
			set(bit::SetBitFlag32(get(), paramFlag, paramFlagValue));
		}
	}

	//--------------------------------------------------------------------------------------
	XGameData::XGameData() :GoldCoin(0), Health(0), Option(0), PassGate(0), GameStatus(GAME_STATUS_NEW)
	{

	}
	//--------------------------------------------------------------------------------------
	bool XGameData::isMusicFlag() const
	{
		return Option.getOptionFlag(OPTION_FLAG_MUSIC);
	}
	//--------------------------------------------------------------------------------------
	void XGameData::setOption_MusicFlag(bool paramFlagValue)
	{
		Option.setOptionFlag(OPTION_FLAG_MUSIC, paramFlagValue);
	}
	//--------------------------------------------------------------------------------------
	int XGameData::PassScore(int paramPassGate)
	{
		return paramPassGate * 500 + 200 * (paramPassGate + 1) * paramPassGate / 2;
	}

	//--------------------------------------------------------------------------------------

	void XGameData::LoadData()
	{
		XAnsiString strTemp;
		CCUserDefault * pData = CCUserDefault::sharedUserDefault();
		strTemp = pData->getStringForKey("PassGate").c_str();
		PassGate = strTemp.ToIntDef(0);
		int iPass = PassGate;
		if (iPass < 1 || iPass > 100)
		{
			iPass = 1;
		}
		PassGate = iPass;
		strTemp = pData->getStringForKey("GoldCoin").c_str();
		GoldCoin = strTemp.ToLongDef(0);
		if (GoldCoin < 0) GoldCoin = 0;

		strTemp = pData->getStringForKey("Health").c_str();
		Health = strTemp.ToIntDef(0);
		if (Health < 0 || Health > 100) Health = 0;
		
		strTemp = pData->getStringForKey("Option").c_str();
		Option.set(strTemp.ToDWordDef(0));

		strTemp = pData->getStringForKey("Score").c_str();
		Score = strTemp.ToIntDef(0);
		if (Score < 0) Score = 0;

		strTemp = pData->getStringForKey("BaseScore").c_str();
		BaseScore = strTemp.ToIntDef(0);
		if (BaseScore < 0) BaseScore = 0;

		strTemp = pData->getStringForKey("AwardScore").c_str();
		AwardScore = strTemp.ToIntDef(0);
		if (AwardScore < 0) AwardScore = 0;

		strTemp = pData->getStringForKey("HighScore").c_str();
		HighScore = strTemp.ToIntDef(0);
		if (HighScore < 0) HighScore = 0;

		strTemp = pData->getStringForKey("GameStatus").c_str();
		GameStatus = strTemp.ToIntDef(0);
		if (GameStatus < 0 || GameStatus > 10) GameStatus = 0;

		strTemp = pData->getStringForKey("ResumeData").c_str();
		base64::DecodeBase64(strTemp, StarBuffer);
	}


	#define SAVE_DATA(DT) {XAnsiString s(DT.get());\
		CCUserDefault::sharedUserDefault()->setStringForKey(#DT, s.c_str());\
	}
	//--------------------------------------------------------------------------------------
	void XGameData::SaveData()
	{
		SAVE_DATA(PassGate);
		SAVE_DATA(GoldCoin);
		SAVE_DATA(Health);
		SAVE_DATA(Option);
		SAVE_DATA(Score);
		SAVE_DATA(GameStatus);
		SAVE_DATA(BaseScore);
		SAVE_DATA(AwardScore);
		SAVE_DATA(HighScore);
		if (GameStatus != XGameData::GAME_STATUS_RESUME)
		{
			this->StarBuffer->Clear();
		}
		XBuffer stTemp;
		base64::EncodeBase64(StarBuffer, stTemp);
		stTemp.AppendByte(0);
		CCUserDefault::sharedUserDefault()->setStringForKey("ResumeData", (const char *)stTemp->getData());
		CCUserDefault::sharedUserDefault()->flush();
	}
	//--------------------------------------------------------------------------------------
	int XGameData::GetAwardCleanScore(int paramCleanCount)
	{
		int iRet = 0;
		if (paramCleanCount >= 3)
		{
			float p = paramCleanCount;
			//iRet = static_cast<int>((p * p) / 10 - 1) * 10;
			iRet = static_cast<int>((p * (p + 1)) / 10) * 10;
		}
		return iRet;
	}
	//--------------------------------------------------------------------------------------
	int XGameData::GetBaseCleanScore(int paramCleanCount)
	{
		return paramCleanCount * 10;
	}
	//--------------------------------------------------------------------------------------
	int XGameData::GetRemainScore(int paramRemainCount)
	{
		int iRet = 1000 - paramRemainCount * 50;
		if (iRet < 0) iRet = 0;
		return iRet;
	}

}
